package com.lw.edu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lw.commonutil.result.Result;
import com.lw.commonutil.vo.CourseVo;
import com.lw.edu.entity.Course;
import com.lw.edu.entity.vo.CourseInfoVo;
import com.lw.edu.entity.vo.CoursePublishVo;
import com.lw.edu.entity.vo.CourseQuery;
import com.lw.edu.service.CourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/admin/edu/course")
public class CourseController {

    @Autowired
    private CourseService courseService;
    //添加课程基本信息 ，操作course和courseDescription两张表

    //查询课程列表 条件查询带分页
    @GetMapping("/list/{current}/{limit}")
    public Result getList(@PathVariable long current,
                          @PathVariable long limit,
                          CourseQuery courseQuery){
        Page page = new Page( current, limit );
        Page<Course> pageList = courseService.pageList(page,courseQuery);
        return Result.ok(pageList);
    }

    @PostMapping("/addCourseInfo")
    public Result addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        String id = courseService.addCourseInfo(courseInfoVo);
        //返回课程id
        return Result.ok(id);
    }
    //根据课程id查找课程信息
    @GetMapping("/getCourseInfo/{courseId}")
    public Result getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);
        return Result.ok( courseInfoVo );
    }
    //修改课程信息
    @PostMapping("/updateCourseInfo")
    public Result updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfo(courseInfoVo);
        return Result.ok();
    }
    //根据课程id查询课程确认信息
    @GetMapping("/getCoursePublish/{courseId}")
    public Result getCoursePublish(@PathVariable String courseId){
        CoursePublishVo coursePublishVo = courseService.getCoursePublish(courseId);
        return Result.ok( coursePublishVo );

    }
    //最终发布
    //修改课程状态
    @PostMapping("/publishCourse/{id}")
    public Result publishCourse(@PathVariable String id){
        //根据id查询课程
        Course course = courseService.getById( id );
        course.setStatus( "Normal" );
        boolean b = courseService.updateById( course );
        if (b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    //删除课程
    @DeleteMapping("/{courseId}")
    public Result deleteCourse(@PathVariable String courseId){
        boolean b = courseService.deleteCourse( courseId );
        if (b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

}
